package com.cskaoyan.service.back;

import com.cskaoyan.bean.po.MarketPermission;
import com.cskaoyan.bean.po.MarketPermissionExample;
import com.cskaoyan.mapper.MarketPermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @author: jzc
 * @since: 2022/10/14 16:36
 **/
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    MarketPermissionMapper permissionMapper;

    /**
     * 角色授权操作
     * @param roleId 要授权角色的id
     * @param permissions 要授权的信息
     */
    @Transactional
    @Override
    public void options(Integer roleId, List<String> permissions) {
        // 首先查询此角色已授权的权限
        MarketPermissionExample permissionExample = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = permissionExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        List<MarketPermission> marketPermissions = permissionMapper.selectByExample(permissionExample);
        // 遍历list与已授权的信息作比较，若存在则保持不变，若不存在则添加，剩余的修改 删除状态为已删除状态
        // 遍历list
        for (int i = 0; i < permissions.size(); i++) {
            // 遍历已授权的信息作比较
            Iterator<MarketPermission> iterator = marketPermissions.iterator();
            while (iterator.hasNext()) {
                MarketPermission marketPermission = iterator.next();
                String permission1 = marketPermission.getPermission();
                // 判断是否相等
                String permission = permissions.get(i);
                if (permission.equals(permission1)){
                    // 若存在从List<MarketPermission> marketPermissions和List<String> permissions中都移除此条数据
                    iterator.remove();
                    permissions.remove(permission);
                    // 删除的状态是false的时候不做修改；是true的时候修改删除的状态和修改时间，将此条记录修改至数据库
                    if (marketPermission.getDeleted()){
                        // 修改删除状态和修改时间
                        marketPermission.setDeleted(false);
                        marketPermission.setUpdateTime(new Date());
                        // 修改数据库的记录
                        permissionMapper.updateByPrimaryKey(marketPermission);
                    }
                }
            }
        }

        // 遍历完若有不存在的就直接添加一条记录
        if (permissions.size()>0){
            for (String permission : permissions) {
                MarketPermission marketPermission = new MarketPermission();
                marketPermission.setRoleId(roleId);
                marketPermission.setPermission(permission);
                Date date = new Date();
                marketPermission.setAddTime(date);
                marketPermission.setUpdateTime(date);
                marketPermission.setDeleted(false);
                permissionMapper.insert(marketPermission);
            }
        }
        // 然后将list中剩余的记录的删除状态修改为true，表示删除
        Iterator<MarketPermission> iterator = marketPermissions.iterator();
        while (iterator.hasNext()) {
            MarketPermission permission = iterator.next();
            // 修改修改时间
            permission.setUpdateTime(new Date());
            permission.setDeleted(true);
            // 然后修改数据
            permissionMapper.updateByPrimaryKey(permission);
        }
    }
}
